<template>
  <div id="QuestionSubmitsView">
    <a-space align="center">
      <a-form :model="searchParams" layout="inline">
        <a-form-item field="questionId" label="名称" style="min-width: 280px">
          <a-input v-model="searchParams.questionId" placeholder="请输入题号" />
        </a-form-item>
        <a-form-item field="name" label="编程语言" style="min-width: 280px">
          <a-select v-model="searchParams.language" placeholder="语言">
            <a-option value="java">Java</a-option>
            <a-option value="c">C</a-option>
            <a-option value="cpp">C++</a-option>
            <a-option value="html">html</a-option>
          </a-select>
        </a-form-item>
        <a-form-item>
          <a-button type="primary" @click="handleSearch">
            <icon-search />
            搜索
          </a-button>
        </a-form-item>
      </a-form>
      <a-button type="primary" @click="loadData">
        <!--TODO 调整-->
        <icon-refresh />
      </a-button>
    </a-space>
    <a-divider size="0" />
    <!---------------题目表格------------------------------>
    <a-table
      :columns="columns"
      :data="dataList"
      :pagination="{
        showTotal: true,
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        total,
      }"
      page-position="bottom"
      @page-change="handlePageChange"
      @page-size-change="handlePageSizeChange"
    >
      <!-- 自定义空状态 -->
      <template #empty>
        <div style="text-align: center; padding: 40px">
          <img
            alt="Empty Data"
            src="@/assets/background3.png"
            style="width: 100px"
          />
          <p style="color: #999">暂无数据，请尝试其他筛选条件。</p>
        </div>
      </template>
      <!---------------题目------------------------------>
      <template #question="{ record }">
        <a-link :href="`/view/question/${record.questionVO.id}`">
          {{ record.questionVO.title }}
        </a-link>
      </template>
      <!---------------判题信息------------------------------>
      <template #judgeInfo="{ record }">
        {{ record.judgeInfo }}
      </template>
      <!---------------题目内容单元格------------------------------>
      <template #content="{ record }">
        <label @click="currentModal = record">
          {{ record.content }}
        </label>
        <a-modal
          :mask-closable="true"
          :mask-style="{
            background: 'rgba(180,180,180,0.04)',
          }"
          :visible="currentModal"
          hide-cancel
          status="info"
          title="题目内容"
          @cancel="currentModal = 0"
          @ok="currentModal = 0"
        >
          <MdEditor :mode="'preview'" :value="record.content"></MdEditor>
        </a-modal>
      </template>
      <!---------------提交时间------------------------------>
      <template #createTime="{ record }">
        {{ moment(record.createTime).format("YYYY-MM-DD") }}
      </template>
    </a-table>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref, watchEffect } from "vue";
import { Modal } from "@arco-design/web-vue";
import {
  Question,
  QuestionControllerService,
  QuestionSubmitQueryRequest,
} from "../../../api";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import MdEditor from "@/components/MdEditor.vue";
import moment from "moment";

const dataList = ref([]);
const total = ref(0);
const searchParams = ref<QuestionSubmitQueryRequest>({
  current: 1,
  pageSize: 10,
  language: "",
});
const $modal = Modal;
const columns = [
  {
    title: "提交号",
    dataIndex: "id",
    ellipsis: true,
    tooltip: { position: "bottom" },
  },
  {
    title: "题目",
    dataIndex: "questionVO",
    slotName: "question",
  },
  {
    title: "编程语言",
    dataIndex: "language",
  },
  {
    title: "判题状态",
    dataIndex: "status",
  },
  {
    title: "判题信息",
    dataIndex: "judgeInfo.message",
    slotName: "judgeInfo",
    ellipsis: true,
    tooltip: { position: "bottom" },
  },
  {
    title: "提交者",
    dataIndex: "userVO.userName",
  },
  {
    title: "提交时间",
    dataIndex: "createTime",
    slotName: "createTime",
  },
];
const router = useRouter();
const currentModal = ref(0);
/**
 * 加载数据
 */
const loadData = async () => {
  const rs = await QuestionControllerService.listQuestionSubmitByPageUsingPost({
    ...searchParams.value,
    sortField: "createTime",
    sortOrder: "desc",
  });
  if (rs.code === 0) {
    dataList.value = rs.data.records;
    total.value = rs.data.total;
  } else {
    message.error("获取加载失败" + rs.message);
  }
};

/**
 * 修改
 * @param question
 */
const doQuestionPage = (question: Question) => {
  router.push({
    path: `/view/question/${question.id}`,
  });
};

/**
 * 处理页面更换
 * @param current
 */
const handlePageChange = (current: number) => {
  searchParams.value.current = current;
};
/**
 * 修改页内数量大小
 * @param size
 */
const handlePageSizeChange = (size: number) => {
  searchParams.value.pageSize = size;
};
/**
 * 处理搜索
 */
const handleSearch = () => {
  searchParams.value = {
    ...searchParams.value,
    current: 1,
  }; //重置当前页
};

/**
 * 页面加载时触发数据加载
 */
onMounted(() => {
  console.log();
});

/**
 * 监听searchParam的变化,改变时触发页面的数据加载
 */
watchEffect(() => {
  let { current } = searchParams.value; //显式调用变量,使监控能够识别到
  loadData();
});
</script>

<style scoped>
#QuestionSubmitsView {
  width: 1280px;
  margin: 0 auto;
}
</style>
